package cn.cttic.app.service.impl;

import cn.cttic.app.mapper.BaseCompanyMapper;
import cn.cttic.base.api.domain.BaseProduct;
import cn.cttic.base.api.domain.bo.BaseProductBo;
import cn.cttic.base.api.domain.vo.BaseProductVo;
import cn.cttic.common.satoken.utils.AppLoginHelper;
import cn.hutool.core.bean.BeanUtil;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.app.mapper.BaseProductMapper;
import cn.cttic.app.service.IBaseProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品套餐管理Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class BaseProductServiceImpl implements IBaseProductService {

    private final BaseProductMapper baseMapper;

    /**
     * 查询产品套餐管理
     */
    @Override
    public BaseProductVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品套餐管理列表
     */
    @Override
    public TableDataInfo<BaseProductVo> queryPageList(BaseProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseProduct> lqw = buildQueryWrapper(bo);
        String appLoginUser = AppLoginHelper.getAppLoginUser().getUserId();
        Page<BaseProductVo> result = baseMapper.getProductPageByUser(appLoginUser, pageQuery.build());
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品套餐管理列表
     */
    @Override
    public List<BaseProductVo> queryList(BaseProductBo bo) {
        LambdaQueryWrapper<BaseProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseProduct> buildQueryWrapper(BaseProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseProduct> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), BaseProduct::getProductName, bo.getProductName());
        lqw.eq(bo.getDeviceId() != null, BaseProduct::getDeviceId, bo.getDeviceId());
        return lqw;
    }

    /**
     * 新增产品套餐管理
     */
    @Override
    public Boolean insertByBo(BaseProductBo bo) {
        BaseProduct add = BeanUtil.toBean(bo, BaseProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品套餐管理
     */
    @Override
    public Boolean updateByBo(BaseProductBo bo) {
        BaseProduct update = BeanUtil.toBean(bo, BaseProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品套餐管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
